﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using View.Control;
using DevExpress.XtraCharts;
using TestMySqlConnetPoll;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;
using DevExpress.XtraSplashScreen;
using System.Threading;
using DevExpress.XtraEditors;

namespace View.Chart
{
    /// <summary>
    /// 曲线控件
    /// </summary>
    public partial class MultiPaneChartBase : XtraUserControl
    {
        //默认设备类型
        public string strDefaultDeviceType;
        //默认设备名称
        public string strDefaultDeviceName;

        private DailyReportTable drTable;   //日报表
        private AnalogTable anTable;        //日曲线
        private KGL_Table kglTable;
        private XYDiagram diagram;
        public ChartType_Data CType;

        private string strNowDType;
        private string strNowDName;
        private string strNowAName;

        private int MNLChart_Count;

        private List<ListViewItem> mnl_list = new List<ListViewItem>();
        private List<ListViewItem> kgl_list = new List<ListViewItem>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public MultiPaneChartBase()
        {
            InitializeComponent();
            // InitializeChart();
            drTable = new DailyReportTable();
            anTable = new AnalogTable();
            kglTable = new KGL_Table();
        }

        private string GetChartName()
        {
            switch(CType)
            {
                case ChartType_Data.Real:
                    return "实时曲线";
                case ChartType_Data.DAY:
                    return "日曲线";
                case ChartType_Data.MONTH:
                    return "月曲线";
                case ChartType_Data.YEAR:
                    return "年曲线";
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="isReal">是否是实时曲线</param>
        public void IniLayout()
        {
            ((XYDiagram)chartControl1.Diagram).DefaultPane.ScrollBarOptions.XAxisScrollBarVisible = false;
            ((XYDiagram)chartControl1.Diagram).DefaultPane.BackColor = Color.Black;//Color.FromArgb(64, 64, 64)
            ((XYDiagram)chartControl1.Diagram).DefaultPane.BorderColor = Color.Black;
            ((XYDiagram)chartControl1.Diagram).AxisX.Label.TextColor = Color.Green;
            ((XYDiagram)chartControl1.Diagram).AxisY.Visible = false;

            switch (CType)
            {
                case ChartType_Data.Real:
                    this.btnPauseResume.Visible = true;
                    this.barEdit_DateTime.Visible = false;
                    this.barEditItem_MYTime.Visible = false;
                    this.lblTime.Visible = false;
                    break;
                case ChartType_Data.DAY:
                    this.btnPauseResume.Visible = false;
                    this.barEdit_DateTime.Visible = true;
                    this.barEditItem_MYTime.Visible = false;
                    this.lblTime.Visible = true;
                    break;
                case ChartType_Data.MONTH:
                    for (int i = -1; i >= -36; i--)
                    {
                        barEditItem_MYTime.Items.Add(DateTime.Now.AddMonths(i).ToString("y"));
                    }
                    barEditItem_MYTime.SelectedIndex = 0;
                    this.btnPauseResume.Visible = false;
                    this.barEdit_DateTime.Visible = false;
                    this.barEditItem_MYTime.Visible = true;
                    this.lblTime.Visible = true;
                    chartControl1.Legend.UseCheckBoxes = true;
                    break;
                case ChartType_Data.YEAR:
                    barEditItem_MYTime.Items.Add(DateTime.Now.ToString("yyyy") + "年");
                    barEditItem_MYTime.Items.Add(DateTime.Now.AddYears(-1).ToString("yyyy") + "年");
                    barEditItem_MYTime.Items.Add(DateTime.Now.AddYears(-2).ToString("yyyy") + "年");
                    barEditItem_MYTime.SelectedIndex = 0;
                    this.btnPauseResume.Visible = false;
                    this.barEdit_DateTime.Visible = false;
                    this.barEditItem_MYTime.Visible = true;
                    this.lblTime.Visible = true;
                    chartControl1.Legend.UseCheckBoxes = true;
                    break;
                default:
                    break;
            }

            InitializeChart();
        }

        /// <summary>
        /// 初始化曲线控件
        /// </summary>
        private void InitializeChart()
        {
            chartControl1.CrosshairOptions.ShowArgumentLine = true;
            chartControl1.CrosshairOptions.ShowValueLine = true;
            chartControl1.MouseWheel += ChartControl_MouseWheel;
            chartControl1.CustomDrawAxisLabel += ChartContro_CustomDrawAxisLabel;

            diagram = (XYDiagram)chartControl1.Diagram;
            chartControl1.CrosshairOptions.CrosshairLabelMode = CrosshairLabelMode.ShowForEachSeries;
            if (CType == ChartType_Data.DAY)
            {
                diagram.EnableAxisXZooming = true;
                diagram.EnableAxisXScrolling = true;
            }
            else
            {
                diagram.EnableAxisXZooming = false;
                diagram.EnableAxisXScrolling = false;
            }
            SetAxisX();
        }

        /// <summary>
        /// 自定义坐标显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ChartContro_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            if (CType == ChartType_Data.Real)    //实时曲线
            {
                string strLbl = e.Item.Text;
                string[] lblArr = strLbl.Split(':');

                if (lblArr.Length == 3)
                    if (lblArr[2] == "00")
                    {
                        e.Item.Text = lblArr[0] + ":" + lblArr[1];
                    }
                    else
                    {
                        e.Item.Text = lblArr[2];
                    }

            }
            else if (CType == ChartType_Data.DAY) //日曲线
            {
                XYDiagram diagram = ((ChartControl)sender).Diagram as XYDiagram;

                if (e.Item.AxisValue is DateTime)
                {
                    DateTime time = (DateTime)e.Item.AxisValue;

                    if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Second)
                    {
                        if (time.Second != 0)
                        {
                            e.Item.Text = time.Second.ToString();
                        }
                        else
                        {
                            e.Item.Text = time.ToString("HH:mm");
                        }
                    }
                    else if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Minute)
                    {
                        if (time.Second == 0 && time.Minute == 0)
                        {
                            e.Item.Text = time.ToString("HH:mm");
                        }
                        else
                        {
                            e.Item.Text = time.Minute.ToString();
                        }
                    }
                    else if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Hour)
                    {
                        if (time.Second == 0 && time.Minute == 0 && time.Hour == 0)
                        {
                            e.Item.Text = time.ToString("MM-dd");
                        }
                        else
                        {
                            e.Item.Text = time.Hour.ToString();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 设置曲线滚动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ChartControl_MouseWheel(object sender, MouseEventArgs e)
        {
            if (this.CType == ChartType_Data.DAY)
            {
                ((ChartControl)sender).BeginInit();
                XYDiagram diagram = ((ChartControl)sender).Diagram as XYDiagram;

                double minValueX = diagram.AxisX.VisualRange.MinValueInternal;
                double maxValueX = diagram.AxisX.VisualRange.MaxValueInternal;

                DateTime minValueX_O = (DateTime)diagram.AxisX.VisualRange.MinValue;
                DateTime maxValueX_O = (DateTime)diagram.AxisX.VisualRange.MaxValue;

                double scrollMinValueX = diagram.AxisX.WholeRange.MinValueInternal;
                double scrollMaxValueX = diagram.AxisX.WholeRange.MaxValueInternal;

                DateTime scrollMinValueX_O = (DateTime)diagram.AxisX.WholeRange.MinValue;
                DateTime scrollMaxValueX_O = (DateTime)diagram.AxisX.WholeRange.MaxValue;

                DiagramCoordinates coord = diagram.PointToDiagram(e.Location);
                double x = coord.NumericalArgument;
                double y = coord.NumericalValue;

                DiagramCoordinates coord_O = diagram.PointToDiagram(e.Location);
                DateTime x_o = coord.DateTimeArgument;

                //&&  maxValueX - minValueX > 0.1
                if (e.Delta < 0)
                {
                    if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Second)
                    {
                        if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 1)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 10;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-5 * 15), x_o.AddSeconds(5 * 15));

                            //diagram.AxisX.VisualRange.SetMinMaxValues(0.2 * x + 0.8 * minValueX, 0.2 * x + 0.8 * maxValueX);
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 10)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 30;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-15 * 15), x_o.AddSeconds(15 * 15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 30)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-30 * 15), x_o.AddSeconds(30 * 15));
                        }
                    }
                    else if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Minute)
                    {
                        if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 1)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 10;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-300 * 15), x_o.AddSeconds(300 * 15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 10)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 30;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-900 * 15), x_o.AddSeconds(900 * 15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 30)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Hour;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-1800 * 15), x_o.AddSeconds(1800 * 15));
                        }
                    }
                    else if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Hour)
                    {
                        if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 1)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Hour;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 2;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-60 * 60 * 15), x_o.AddSeconds(60 * 60 * 15));
                        }
                    }
                }
                if (e.Delta > 0)//&& (minValueX >= scrollMinValueX || maxValueX <= scrollMinValueX)
                {
                    if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Second)
                    {
                        if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 1)
                        {
                            //diagram.AxisX.DateTimeScaleOptions.GridSpacing = 10;
                            //diagram.AxisX.VisualRange.SetMinMaxValues(minValueX_O, minValueX_O.AddSeconds(10 * 15));

                            //diagram.AxisX.VisualRange.SetMinMaxValues(0.2 * x + 0.8 * minValueX, 0.2 * x + 0.8 * maxValueX);
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 10)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-15), x_o.AddSeconds(15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 30)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Second;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 10;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-5 * 15), x_o.AddSeconds(5 * 15));
                        }
                    }
                    else if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Minute)
                    {
                        if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 1)
                        {
                            //diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Second;
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Second;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 30;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-15 * 15), x_o.AddSeconds(15 * 15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 10)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-30 * 15), x_o.AddSeconds(30 * 15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 30)
                        {
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 10;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-5 * 60 * 15), x_o.AddSeconds(5 * 60 * 15));
                        }
                    }
                    else if (diagram.AxisX.DateTimeScaleOptions.GridAlignment == DateTimeGridAlignment.Hour)
                    {
                        if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 1)
                        {
                            //diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 30;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-15 * 60 * 15), x_o.AddSeconds(15 * 60 * 15));
                        }
                        else if (diagram.AxisX.DateTimeScaleOptions.GridSpacing == 2)
                        {

                            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Hour;
                            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
                            diagram.AxisX.VisualRange.SetMinMaxValues(x_o.AddSeconds(-30 * 60 * 15), x_o.AddSeconds(30 * 60 * 15));
                        }
                    }
                }
                ((ChartControl)sender).EndInit();
            }
        }

        /// <summary>
        /// 设置X轴显示
        /// </summary>
        private void SetAxisX()
        {
            diagram.AxisX.GridLines.Visible = true;
            diagram.AxisX.GridLines.Color = Color.FromArgb(33, 33, 33);
            diagram.AxisX.GridLines.LineStyle.DashStyle = DevExpress.XtraCharts.DashStyle.Dash;
            diagram.AxisX.Tickmarks.Visible = true;

            diagram.AxisX.WholeRange.AlwaysShowZeroLevel = true;
            diagram.AxisX.WholeRange.AutoSideMargins = false;
            diagram.AxisX.WholeRange.SideMarginsValue = 0;

            if (this.CType == ChartType_Data.Real)
            {
                diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Second;
                diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Second;
                diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;
                diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
                diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
            }
            //diagram.AxisX.DateTimeScaleOptions.GridOffset = 100;

            //diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
            //diagram.AxisX.DateTimeScaleOptions.GridSpacing = 1;
            //diagram.AxisX.DateTimeScaleOptions.GridOffset = 100;
            //diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Second;
            //diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
            //diagram.AxisX.DateTimeScaleOptions.GridOffset = 50;
            //diagram.AxisX.Label.Visible = true;
            //diagram.AxisX.VisualRange.Auto = false;
            //diagram.AxisX.WholeRange.Auto = false;
            //diagram.AxisX.Label.DateTimeOptions.Format = DateTimeFormat.Custom;
            //diagram.AxisX.Label.DateTimeOptions.FormatString = "HH:mm:ss";
        }

        /// <summary>
        /// 属性列表选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListView_Pro_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked)
            {
                strNowAName = e.Item.SubItems[1].Text;

                if ((e.Item.Tag as string) == "MNL")
                {
                    if (mnl_list.Count < 3)
                    {
                        string strPaneNameNew = e.Item.SubItems[1].Text;
                        MNLChart_Count++;
                        AddChart(strPaneNameNew, true);
                        mnl_list.Add(e.Item);

                    }
                    else
                    {
                        ListViewItem temItem = mnl_list[0];
                        string strPaneNameOld = temItem.SubItems[1].Text;
                        mnl_list.RemoveAt(0);
                        DelChart(strPaneNameOld);

                        string strPaneNameNew = e.Item.SubItems[1].Text;
                        AddChart(strPaneNameNew, true);
                        temItem.Checked = false;
                        mnl_list.Add(e.Item);
                    }
                }

                if ((e.Item.Tag as string) == "KGL")
                {
                    if (kgl_list.Count < 3)
                    {
                        kgl_list.Add(e.Item);
                        string strPaneNameNew = e.Item.SubItems[1].Text;
                        AddChart(strPaneNameNew, false);
                    }
                    else
                    {
                        ListViewItem temItem = mnl_list[0];
                        string strPaneNameOld = temItem.SubItems[1].Text;
                        kgl_list.RemoveAt(0);
                        DelChart(strPaneNameOld);

                        string strPaneNameNew = e.Item.SubItems[1].Text;
                        kgl_list.Add(e.Item);
                        AddChart(strPaneNameNew, false);
                        temItem.Checked = false;
                    }
                }
            }
            else
            {
                if ((e.Item.Tag as string) == "MNL")
                {
                    if (mnl_list.Count != 0)
                        MNLChart_Count--;
                    if (mnl_list.Contains(e.Item))
                        mnl_list.Remove(e.Item);

                    string strPaneNameOld = e.Item.SubItems[1].Text;
                    DelChart(strPaneNameOld);
                }
                else
                {
                    if (kgl_list.Contains(e.Item))
                        kgl_list.Remove(e.Item);

                    string strPaneNameOld = e.Item.SubItems[1].Text;
                    DelChart(strPaneNameOld);
                }
            }

            if (mnl_list.Count <= 0 && kgl_list.Count <= 0)
            {
                ((XYDiagram)chartControl1.Diagram).DefaultPane.Visible = true;
                ((XYDiagram)chartControl1.Diagram).AxisX.Visible = false;
                ((XYDiagram)chartControl1.Diagram).AxisX.GridLines.Visible = false;
            }else if(mnl_list.Count <= 0 && kgl_list.Count >0)
            {
                ((XYDiagram)chartControl1.Diagram).DefaultPane.Visible = true;
                ((XYDiagram)chartControl1.Diagram).AxisX.Visible = true; 
                ((XYDiagram)chartControl1.Diagram).AxisX.GridLines.Visible = true; 
            }
            else
            {
                ((XYDiagram)chartControl1.Diagram).DefaultPane.Visible = false;
                ((XYDiagram)chartControl1.Diagram).AxisX.Visible = true;
                ((XYDiagram)chartControl1.Diagram).AxisX.GridLines.Visible = true;
            }
        }

        /// <summary>
        /// 添加曲线
        /// </summary>
        /// <param name="strChartName">曲线名称</param>
        /// <param name="isMNL">是否是模拟量类型曲线</param>
        private void AddChart(string strChartName, bool isMNL)
        {
            ChartPro pro = new ChartPro();
            pro.strYLable = strChartName;

            if (isMNL)
                CreatMNLChart(pro, strChartName);
            else
                CreateKGLChart(pro, strChartName);
        }

        private void DelChart(string strChartName)
        {
            //try
            //{
            //    ThreadList[strChartName].Abort();
            //}
            //catch { }
            if (paneDic.ContainsKey(strChartName))
            {
                RemoveChartPanel(paneDic[strChartName]);
                paneDic.Remove(strChartName);
            }
        }

        //曲线更新Thread集合
        private Dictionary<string, Thread> ThreadList = new Dictionary<string, Thread>();
        //曲线Pane集合
        private Dictionary<string, MultiChartPane> paneDic = new Dictionary<string, MultiChartPane>();

        /// <summary>
        /// 创建实时数据更新线程
        /// </summary>
        private void CreateRealSeriesTimer(string strKey)
        {
            Thread timer = new Thread(ThreadPoolCallback_SeriesUpdata);
            timer.Start(paneDic[strKey]);
            ThreadList.Add(strKey, timer);
        }

        /// <summary>
        /// 更新线程
        /// </summary>
        /// <param name="threadContext"></param>
        private void ThreadPoolCallback_SeriesUpdata(Object threadContext)
        {
            MultiChartPane pro = (MultiChartPane)threadContext;
            List<SeriesPoint> pointsToUpdate = new List<SeriesPoint>();

            ObjValue value = c_RealTimeValue.GetRealTimeAnalogValue(strNowDType, strNowDName, strNowAName);
        }


        private Series IniSeries(SecondaryAxisY myAxisY, XYDiagramPane pane, List<Series> SeriesForMY, string strPaneName, objSingleAnalogStruct analogStruce, string strYLabel)
        {
            Series series = new Series();
            series.Name = strPaneName;
            series.ArgumentScaleType = ScaleType.DateTime;
            series.ValueScaleType = ScaleType.Numerical;
            series.Label.Border.Visible = false;
            series.Label.BackColor = Color.Transparent;


            series.ChangeView(ViewType.Line);

            if (CType == ChartType_Data.MONTH || CType == ChartType_Data.YEAR)
            {
                series.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.False;
                series.CheckableInLegend = true;
            }
            else
            {
                series.CheckableInLegend = false;
                series.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.True;
                series.CrosshairLabelVisibility = DevExpress.Utils.DefaultBoolean.True;
                series.CrosshairHighlightPoints = DevExpress.Utils.DefaultBoolean.True;
                series.CrosshairHighlightPoints = DevExpress.Utils.DefaultBoolean.True;
                series.CrosshairLabelPattern = String.Format("时间:{{A}}{0}{1}:{{V:F2}}", System.Environment.NewLine, strPaneName);
            }
            series.LegendText = strPaneName;
            series.ToolTipEnabled = DevExpress.Utils.DefaultBoolean.True;

            chartControl1.Series.Add(series);
            SeriesForMY.Add(series);

            SetAxisForSeries(SeriesForMY, myAxisY, series, pane, strYLabel, analogStruce, strPaneName);//pro.strXLable
            return series;
        }

        /// <summary>
        /// 获取月份中的某一天
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        private int GetMonthDay(int year, int month)
        {
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                case 2:
                    return (28 + (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)).GetHashCode());
                default:
                    break;
            }

            return 0;

        }

        /// <summary>
        /// 生成模拟量图表
        /// </summary>
        private void CreatMNLChart(ChartPro pro, string strPaneName)
        {
            float MaxValue = 0;
            float MinValue = 0;
            float LowerThreshold = 0;
            float UpperThreshold = 0;
            DateTime time_sel = DateTime.MinValue;
            XYDiagramPane pane = null;
            SecondaryAxisY myAxisY = null;

            if (this.CType == ChartType_Data.MONTH || this.CType == ChartType_Data.YEAR)
            {
                time_sel = DateTime.Parse((string)barEditItem_MYTime.SelectedItem);
            }

            //初始化Series

            List<Series> SeriesForMY = new List<Series>();

            if (CType == ChartType_Data.DAY)
                goto DAY;
            else if (CType == ChartType_Data.MONTH)
                goto MONTH;
            else if (CType == ChartType_Data.YEAR)
                goto YEAR;
            else if (CType == ChartType_Data.Real)
                goto REAL;

        DAY:
            diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Hour;
            diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;
            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 2;

            //查询日曲线数据
            anTable.ReadDailyValueList(strNowDType, strNowDName, strPaneName, this.barEdit_DateTime.Value);
            while (!anTable.Ready_DailyCurveValueList)
            {
                //SplashScreenManager.Default.SetWaitFormDescription("执行时间" + DateTime.Now.Subtract(dt).Hours + ":" + DateTime.Now.Subtract(dt).Minutes + ":" + DateTime.Now.Subtract(dt).Seconds);
                if (anTable.ErrorFlag != 0)
                {
                    //MessageBox.Show("查询不到对应数据");
                    goto FINSH;
                    //return;
                }

                Thread.Sleep(20);
            }

            DevClass nowDClass = new DevClass();
            List<SeriesPoint> points = new List<SeriesPoint>();

            if (anTable.DailyCurveValueList.ContainsKey(strNowDName))
            {
                foreach (DevClass dClass in anTable.DailyCurveValueList[strNowDName])
                {
                    if (dClass.AttriName == strPaneName)
                    {
                        nowDClass = dClass;
                        break;
                    }
                }
            }else
            {
                nowDClass.objSet = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName);
            }

            //设值
            foreach (objTimeValue tValue in nowDClass.ValueList)
            {
                SeriesPoint s = new SeriesPoint(MySql.Time_tToDateTime(tValue.u_time), tValue.Tvalue.Value);
                points.Add(s);
            }

            pane = SetPanelForSeries();
            myAxisY = SetAxisYForPanel(pro.strYLable, nowDClass.objSet);
            IniSeries(myAxisY, pane, SeriesForMY, strPaneName, nowDClass.objSet, pro.strYLable);
            AddPoints(SeriesForMY[0], points);
            
            //设置坐标轴初始范围
            DateTime timeX_new = new DateTime(this.barEdit_DateTime.Value.Year, this.barEdit_DateTime.Value.Month, this.barEdit_DateTime.Value.Day, 0, 0, 0);
            diagram.AxisX.WholeRange.Auto = false;
            diagram.AxisX.WholeRange.SetMinMaxValues(timeX_new, timeX_new.AddHours(30));

            diagram.AxisX.VisualRange.Auto = false;
            diagram.AxisX.VisualRange.SetMinMaxValues(timeX_new, timeX_new.AddHours(30));

            MultiChartPane temPane = new MultiChartPane();
            temPane.isKGL = false;
            temPane.strPaneName = strPaneName;
            temPane.pane = pane;
            temPane.myAxisY = myAxisY;
            temPane.SeriesList = SeriesForMY;

            if (!paneDic.ContainsKey(strPaneName))
                paneDic.Add(strPaneName, temPane);
            goto FINSH;

        MONTH:
            //月曲线
            //设置初始曲线X坐标
            int days = GetMonthDay(time_sel.Year, time_sel.Month);
            DateTime timeForX = new DateTime(DateTime.Parse((string)barEditItem_MYTime.SelectedItem).Year,
                                             DateTime.Parse((string)barEditItem_MYTime.SelectedItem).Month, 1);

            diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
            diagram.AxisX.WholeRange.Auto = false;
            diagram.AxisX.WholeRange.SetMinMaxValues(timeForX, timeForX.AddDays(days - 1));

            diagram.AxisX.VisualRange.Auto = false;
            diagram.AxisX.VisualRange.SetMinMaxValues(timeForX, timeForX.AddDays(days - 1));
            diagram.AxisX.Tickmarks.Visible = false;


            DailyReportTable.objMonthCurve mCurve = new DailyReportTable.objMonthCurve();
            objDate data = new objDate();

            DateTime dt = DateTime.Parse((string)barEditItem_MYTime.SelectedItem);
            data.year = dt.Year;
            data.month = (byte)dt.Month;
            data.day = (byte)dt.Day;
            drTable.ReadMonthCurveValueList(data, strNowDType, strNowDName);
            while (!drTable.Ready_MonthCurveValueList)
            {
                if (drTable.MonthErrorFlag != 0)
                {
                    break;
                }

                Thread.Sleep(20);
            }

            if (drTable.MonthCurveValueList.ContainsKey(strNowDName) && drTable.MonthCurveValueList[strNowDName].Count > 0)
            {
                foreach (DailyReportTable.objMonthCurve dClass in drTable.MonthCurveValueList[strNowDName])
                {
                    if (dClass.AttriName == strPaneName)
                    {
                        mCurve = dClass;
                        break;
                    }
                }
            }
            else
            {
                mCurve.objset = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName);
            }
            //月曲线要画3条

            string strHead1 = string.Empty;
            string strHead2 = string.Empty;
            string strHead3 = string.Empty;

            c_Device.GetDailyTableHeadStr(strNowDType, strPaneName, ref strHead1, ref strHead2, ref strHead3);

            pane = SetPanelForSeries();//每个属性创建一个Panel
            myAxisY = SetAxisYForPanel(pro.strYLable, mCurve.objset);
            Series ser = IniSeries(myAxisY, pane, SeriesForMY, strPaneName + strHead1, mCurve.objset, pro.strYLable);
            points = new List<SeriesPoint>();
            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
            {
                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.MaxValue.Value);
                points.Add(s);
            }

            AddPoints(ser, points);

            ser = IniSeries(myAxisY, pane, SeriesForMY, strPaneName + strHead2, mCurve.objset, pro.strYLable);
            points = new List<SeriesPoint>();
            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
            {
                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.MinValue.Value);
                points.Add(s);
            }
            AddPoints(ser, points);

            ser = IniSeries(myAxisY, pane, SeriesForMY, strPaneName + strHead3, mCurve.objset, pro.strYLable);
            points = new List<SeriesPoint>();
            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
            {
                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.AvrValue.Value);
                points.Add(s);
            }
            AddPoints(ser, points);

            temPane = new MultiChartPane();
            temPane.isKGL = false;
            temPane.strPaneName = strPaneName;
            temPane.pane = pane;
            temPane.myAxisY = myAxisY;
            temPane.SeriesList = SeriesForMY;

            if (!paneDic.ContainsKey(strPaneName))
                paneDic.Add(strPaneName, temPane);

            goto FINSH;

        YEAR:
            //年曲线
            timeForX = new DateTime(DateTime.Parse((string)barEditItem_MYTime.SelectedItem).Year, 1, 1);

            diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
            diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
            diagram.AxisX.DateTimeScaleOptions.GridSpacing = 30;
            diagram.AxisX.WholeRange.Auto = false;
            diagram.AxisX.WholeRange.SetMinMaxValues(timeForX, timeForX.AddMonths(12));

            diagram.AxisX.VisualRange.Auto = false;
            diagram.AxisX.VisualRange.SetMinMaxValues(timeForX, timeForX.AddMonths(12));
            diagram.AxisX.Tickmarks.Visible = false;

            points = new List<SeriesPoint>();
            mCurve = new DailyReportTable.objMonthCurve();
            data = new objDate();

            DateTime dt_year = DateTime.Parse((string)barEditItem_MYTime.SelectedItem);
            data.year = dt_year.Year;
            data.month = (byte)dt_year.Month;
            data.day = (byte)dt_year.Day;
            drTable.ReadYearCurveValueList(data, strNowDType, strNowDName);
            while (!drTable.Ready_YearCurveValueList)
            {
                if (drTable.MonthErrorFlag != 0)
                {
                    break;
                }

                Thread.Sleep(20);
            }

            if (drTable.YearCurveValueList.ContainsKey(strNowDName) && drTable.YearCurveValueList[strNowDName].Count > 0)
            {
                foreach (DailyReportTable.objMonthCurve dClass in drTable.YearCurveValueList[strNowDName])
                {
                    if (dClass.AttriName == strPaneName)
                    {
                        mCurve = dClass;
                        break;
                    }
                }
            }
            else
            {
                mCurve.objset = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName);
            }

            strHead1 = string.Empty;
            strHead2 = string.Empty;
            strHead3 = string.Empty;

            c_Device.GetDailyTableHeadStr(strNowDType, strPaneName, ref strHead1, ref strHead2, ref strHead3);

            pane = SetPanelForSeries();//每个属性创建一个Panel
            myAxisY = SetAxisYForPanel(pro.strYLable, mCurve.objset);
            ser = IniSeries(myAxisY, pane, SeriesForMY, strPaneName + strHead1, mCurve.objset, pro.strYLable);
            points = new List<SeriesPoint>();
            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
            {
                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.MaxValue.Value);
                points.Add(s);
            }

            AddPoints(ser, points);

            ser = IniSeries(myAxisY, pane, SeriesForMY, strPaneName + strHead2, mCurve.objset, pro.strYLable);
            points = new List<SeriesPoint>();
            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
            {
                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.MinValue.Value);
                points.Add(s);
            }
            AddPoints(ser, points);

            ser = IniSeries(myAxisY, pane, SeriesForMY, strPaneName + strHead3, mCurve.objset, pro.strYLable);
            points = new List<SeriesPoint>();
            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
            {
                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.AvrValue.Value);
                points.Add(s);
            }
            AddPoints(ser, points);

            temPane = new MultiChartPane();
            temPane.isKGL = false;
            temPane.strPaneName = strPaneName;
            temPane.pane = pane;
            temPane.myAxisY = myAxisY;
            temPane.SeriesList = SeriesForMY;

            if (!paneDic.ContainsKey(strPaneName))
                paneDic.Add(strPaneName, temPane);

            goto FINSH;

        REAL:
            timer_real.Enabled = true;
            diagram.AxisX.WholeRange.Auto = false;
            diagram.AxisX.WholeRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);

            diagram.AxisX.VisualRange.Auto = false;
            diagram.AxisX.VisualRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);

            MaxValue = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName).ADMax;
            MinValue = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName).ADMin;
            LowerThreshold = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName).LowerThreshold;
            UpperThreshold = c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName).UpperThreshold;

            pane = SetPanelForSeries();//每个属性创建一个Panel
            myAxisY = SetAxisYForPanel(pro.strYLable, c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName));
            Series ser_real = IniSeries(myAxisY, pane, SeriesForMY, strPaneName, c_Device.GetSingleAnalogConfig(strNowDType, strNowDName, strPaneName), pro.strYLable);
            //SeriesPoint nextPoint = new SeriesPoint(DateTime.Now, new double[] { c_RealTimeValue.RealTimeValue_Analog[strNowDName] });
            temPane = new MultiChartPane();
            temPane.isKGL = false;
            temPane.strPaneName = strPaneName;
            temPane.pane = pane;
            temPane.myAxisY = myAxisY;
            temPane.SeriesList = SeriesForMY;

            if (!paneDic.ContainsKey(strPaneName))
                paneDic.Add(strPaneName, temPane);
            goto FINSH;

        FINSH:
            int aaa = 0;
            //((XYDiagram)chartControl1.Diagram).DefaultPane.Visible = false;
            //((LineSeriesView)series.View).Pane = ((XYDiagram)chartControl1.Diagram).DefaultPane;
            //((XYDiagram)chartControl1.Diagram).DefaultPane.Visible = false;
        }

        private XYDiagramPane SetPanelForSeries()
        {
            XYDiagramPane pane = new XYDiagramPane();
            pane.ScrollBarOptions.XAxisScrollBarVisible = false;
            pane.BackColor = Color.Black;//Color.FromArgb(64, 64, 64)
            pane.BorderColor = Color.Green;
            int paneIndex = ((XYDiagram)chartControl1.Diagram).Panes.Add(pane);

            return pane;
        }

        private SecondaryAxisY SetAxisYForPanel(string strLabel, objSingleAnalogStruct analogStruce)
        {
            SecondaryAxisY myAxisY = new SecondaryAxisY();
            ((XYDiagram)chartControl1.Diagram).SecondaryAxesY.Add(myAxisY);

            myAxisY.Alignment = AxisAlignment.Near;
            myAxisY.Label.TextColor = Color.Green;
            myAxisY.Color = Color.Green;
            myAxisY.GridLines.Visible = true;
            myAxisY.GridLines.Color = Color.FromArgb(33, 33, 33);
            myAxisY.GridLines.LineStyle.DashStyle = DevExpress.XtraCharts.DashStyle.Dash;
            //myAxisY.Name = analogStruce.UnitName;
            //设Pane显示名
            if (analogStruce.UnitName != string.Empty)
                myAxisY.Title.Text = strLabel + "(" + analogStruce.UnitName + ")";
            else
                myAxisY.Title.Text = strLabel;
            myAxisY.Title.Visible = true;
            myAxisY.Title.TextColor = Color.Yellow;
            myAxisY.Title.Alignment = StringAlignment.Center;
            myAxisY.Title.Antialiasing = true;
            myAxisY.Title.Font = Font = new Font("Tahoma", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(1)));

            //设置曲线最大量程
            myAxisY.WholeRange.Auto = false;
            myAxisY.WholeRange.MaxValue = analogStruce.ADMax;
            myAxisY.WholeRange.MinValue = analogStruce.ADMin;


            //设置曲线上下限
            ConstantLine constantLine_low = new ConstantLine("下限");
            ConstantLine constantLine_Up = new ConstantLine("上限");

            constantLine_low.AxisValue = analogStruce.LowerThreshold; //曲线下限
            constantLine_Up.AxisValue = analogStruce.UpperThreshold;  //曲线上限
            constantLine_low.Visible = true;
            constantLine_low.ShowInLegend = false;
            constantLine_Up.Visible = true;
            constantLine_Up.ShowInLegend = false;
            constantLine_low.Color = Color.Red;
            constantLine_Up.Color = Color.Red;
            constantLine_low.LineStyle.Thickness = 1;
            myAxisY.ConstantLines.Add(constantLine_low);
            myAxisY.ConstantLines.Add(constantLine_Up);
            return myAxisY;
        }

        private void SetAxisForSeries(List<Series> SeriesForMY, SecondaryAxisY myAxisY, Series series, XYDiagramPane pane, string strYLabel, objSingleAnalogStruct analogStruce, string strPaneName)
        {
            if (CType == ChartType_Data.MONTH || CType == ChartType_Data.YEAR)
                ((XYDiagram)chartControl1.Diagram).AxisX.Label.Visible = false;

            ((LineSeriesView)series.View).AxisX = ((XYDiagram)chartControl1.Diagram).AxisX;
            ((LineSeriesView)series.View).LineMarkerOptions.Size = 3;

            //绑定Panes
            ((LineSeriesView)series.View).Pane = pane;

            //设Y坐标
            ((LineSeriesView)series.View).AxisY = myAxisY;
        }


        /// <summary>
        /// 生成开关量图表
        /// </summary>
        /// <param name="pro"></param>
        /// <param name="strPaneName"></param>
        private void CreateKGLChart(ChartPro pro, string strPaneName)
        {
            XYDiagramPane pane = null;
            SecondaryAxisY myAxisY = null;
            List<Series> SeriesForMY = new List<Series>();

            myAxisY = new SecondaryAxisY();
            ((XYDiagram)chartControl1.Diagram).SecondaryAxesY.Add(myAxisY);

            myAxisY.Alignment = AxisAlignment.Near;
            myAxisY.Label.Visible = false;
            myAxisY.Color = Color.Green;
            myAxisY.Tickmarks.Visible = false;
            myAxisY.Tickmarks.MinorVisible = false;

            //初始化Series
            Series series = new Series();
            series.ArgumentScaleType = ScaleType.DateTime;
            series.ValueScaleType = ScaleType.Numerical;
            series.Label.Border.Visible = false;
            series.Label.BackColor = Color.Transparent;
            series.CrosshairLabelVisibility = DevExpress.Utils.DefaultBoolean.False;
            series.ChangeView(ViewType.StepArea);
            series.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.False;
            series.ShowInLegend = false;
            series.Name = strPaneName;

            chartControl1.Series.Add(series);

            SeriesForMY.Add(series);

            if (CType == ChartType_Data.Real)
            {
                timer_real.Enabled = true;
                diagram.AxisX.WholeRange.Auto = false;
                diagram.AxisX.WholeRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);

                diagram.AxisX.VisualRange.Auto = false;
                diagram.AxisX.VisualRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);
            }


            pane = new XYDiagramPane();
            pane.Annotations.AddTextAnnotation(strPaneName, strPaneName);
            pane.ScrollBarOptions.XAxisScrollBarVisible = false;
            pane.BackColor = Color.Black;//Color.FromArgb(64, 64, 64)
            pane.BorderColor = Color.Green;
            ((XYDiagram)chartControl1.Diagram).Panes.Insert(0,pane);

            //设值
            //List<SeriesPoint> points_kgl = new List<SeriesPoint>();
            //List<SeriesPoint> points_kgl_temp = new List<SeriesPoint>();

            //List<kglstuct> kglList = new List<kglstuct>();
            //bool isGetKgl = kglTable.GetKGLStatue(strNowDType, strNowDName, strPaneName, ref kglList);
            //if (!isGetKgl)
            //    return;

            //int index = 0;
            //foreach (kglstuct kgl in kglList)
            //{
            //    SeriesPoint point = new SeriesPoint(MySql.Time_tToDateTime(kgl.u_time), kgl.Statue);
            //    points_kgl.Add(point);
            //    double temValue = 0;

            //    if (index != 0)
            //    {
            //        if (points_kgl[index - 1].Values[0] != points_kgl[index].Values[0])
            //        {
            //            if (points_kgl[index - 1].Values[0] == 0)
            //                temValue = 1;
            //            else if (points_kgl[index - 1].Values[0] == 1)
            //                temValue = 0;

            //            points_kgl_temp.Add(new SeriesPoint(points_kgl[index - 1].DateTimeArgument, temValue));
            //        }
            //    }
            //    index++;
            //}

            //AddPoints(series, points_kgl);
            //AddPoints(series, points_kgl_temp);
            ////设X坐标


            //绑定Pane
            ((LineSeriesView)series.View).AxisX = ((XYDiagram)chartControl1.Diagram).AxisX;
            ((LineSeriesView)series.View).AxisY = myAxisY;
            ((LineSeriesView)series.View).Pane = pane;
            ((LineSeriesView)series.View).Pane.BackColor = Color.FromArgb(64, 64, 64);
            ((LineSeriesView)series.View).Pane.SizeMode = PaneSizeMode.UseSizeInPixels;
            ((LineSeriesView)series.View).Pane.SizeInPixels = 50;


            MultiChartPane temPane = new MultiChartPane();
            temPane.isKGL = true;
            temPane.strPaneName = strPaneName;
            temPane.pane = pane;
            temPane.myAxisY = myAxisY;
            temPane.SeriesList = SeriesForMY;

            if (!paneDic.ContainsKey(strPaneName))
                paneDic.Add(strPaneName, temPane);
        }

        /// <summary>
        /// 添加点
        /// </summary>
        /// <param name="series"></param>
        /// <param name="pointsToUpdate"></param>
        private void AddPoints(Series series, List<SeriesPoint> pointsToUpdate)
        {
            series.Points.AddRange(pointsToUpdate.ToArray());
        }

        /// <summary>
        /// 移除曲线面板
        /// </summary>
        /// <param name="paneIndex">曲线面板序列</param>
        private void RemoveChartPanel(MultiChartPane chartPane)
        {
            ((XYDiagram)chartControl1.Diagram).Panes.Remove(chartPane.pane);
            ((XYDiagram)chartControl1.Diagram).SecondaryAxesY.Remove(chartPane.myAxisY);

            if (chartPane.series != null)
                chartControl1.Series.Remove(chartPane.series);

            chartPane.SeriesList.ForEach(s => chartControl1.Series.Remove(s));

            chartPane.DisposeAll();
        }

        private void CreateProForDevice()
        {
        }

        /// <summary>
        /// 生成设备选择列表
        /// </summary>
        public void CreateDeviceList(string strDefaultDevice)
        {
            //刷新设备属性列表
            TreeListNode focusedNode = null;
            treeList_device.Nodes.Clear();
            foreach (string strKey in c_Device.DeviceMsgGather.Keys)
            {
                TreeListNode rootNode = treeList_device.Nodes.Add(strKey);
                rootNode.SetValue(0, strKey);

                foreach (objMsgDevStruct device in c_Device.DeviceMsgGather[strKey].DevList)
                {
                    (rootNode.Nodes.Add(device.DevName)).SetValue(0, device.DevName);
                }

                if (rootNode.Nodes.Count > 0 && (strKey == strDefaultDevice))
                {
                    focusedNode = rootNode.Nodes[0];
                    //rootNode.Expanded = true;
                    //rootNode.Nodes[0].Selected = true;
                }
            }

            treeList_device.FocusedNode = focusedNode;//treeList_device.Nodes[1].Nodes[0];
        }

        /// <summary>
        /// 检查属性是否要显示
        /// </summary>
        /// <param name="strDType"></param>
        /// <param name="strDName"></param>
        /// <param name="attIndex"></param>
        /// <returns></returns>
        private bool CheckAttiIsVisable(string strDType, string strDName, int attIndex)
        {
            foreach (objMsgDevStruct device in c_Device.DeviceMsgGather[strDType].DevList)
            {
                if (device.DevName == strDName)
                {
                    return device.AnalogSet[attIndex] != -1 ? true : false;
                }
            }

            return false;
        }

        private List<SeriesPoint> tempPoints = new List<SeriesPoint>();

        /// <summary>
        /// 实时曲线定时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Real_Tick(object sender, EventArgs e)
        {
            foreach (KeyValuePair<string, MultiChartPane> values in paneDic)
            {
                SeriesPoint nextPoint = null;
                ObjValue objValue = c_RealTimeValue.GetRealTimeAnalogValue(strNowDType, strNowDName, values.Key);

                //if (objValue.Value == 65535)
                //    nextPoint = new SeriesPoint(DateTime.Now, new double[] { -1 });
                if (objValue.Value != 65535 && objValue.Value != -1F)
                {
                    nextPoint = new SeriesPoint(DateTime.Now, new double[] { objValue.Value });
                    AnalogSeriesAddPoint(this.chartControl1.Series[values.Key], nextPoint);
                }            
            }

            diagram.AxisX.WholeRange.Auto = false;
            diagram.AxisX.WholeRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);

            diagram.AxisX.VisualRange.Auto = false;
            diagram.AxisX.VisualRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);
        }

        void SetPauseResumeButtonText()
        {
            btnPauseResume.Text = timer_real.Enabled ? "暂停" : "继续";
        }

        private void BtnPauseResume_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.timer_real.Enabled = !this.timer_real.Enabled;
            SetPauseResumeButtonText();
        }

        /// <summary>
        /// 实时更新模拟量曲线值
        /// </summary>
        private void AnalogSeriesAddPoint(Series series, SeriesPoint point)
        {
            foreach (SeriesPoint point_temp in series.Points)
                if (point_temp.DateTimeArgument < DateTime.Now - new TimeSpan(0, 0, 0, 1))
                    tempPoints.Add(point);

            if (tempPoints.Count > 0)
                tempPoints.RemoveAt(0);

            foreach (SeriesPoint point_temp in tempPoints)
                series.Points.Remove(point_temp);

            tempPoints.Clear();
            series.Points.AddRange(new SeriesPoint[] { point });

            AdjustChartRange();
        }

        private void AdjustChartRange()
        {
            diagram.AxisX.WholeRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);
            diagram.AxisX.VisualRange.SetMinMaxValues(DateTime.Now.AddSeconds(-30), DateTime.Now);
            //((XYDiagram)chartControl1.Diagram).AxisX.VisualRange.MinValue = ((DateTime)((XYDiagram)chartControl1.Diagram).AxisX.VisualRange.MinValue).AddSeconds(1);
            //((XYDiagram)chartControl1.Diagram).AxisX.VisualRange.MinValue = ((DateTime)((XYDiagram)chartControl1.Diagram).AxisX.VisualRange.MinValue).AddSeconds(1);

            //((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.MinValue = ((DateTime)((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.MinValue).AddSeconds(1);
            //((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.MinValue = ((DateTime)((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.MinValue).AddSeconds(1);
            ////((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.MinValue = DateTime.Now.AddSeconds(1);
            //((XYDiagram)chartControl1.Diagram).AxisX.WholeRange.MaxValue = DateTime.Now.AddSeconds(1);
        }

        /// <summary>
        /// 实时更新开关量曲线值
        /// </summary>
        private void SwitchSeriesAddPoint()
        {
        }

        /// <summary>
        /// 设置选择节点背景色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeList_Device_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
        {
            TreeList node = sender as TreeList;

            if (e.Node == node.FocusedNode)
            {
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                Rectangle r = new Rectangle(e.EditViewInfo.ContentRect.Left, e.EditViewInfo.ContentRect.Top,
                    Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeList_device.Font).Width + 1),
                    Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeList_device.Font).Height));
                e.Graphics.FillRectangle(SystemBrushes.Highlight, r);
                e.Graphics.DrawString(e.CellText, treeList_device.Font, SystemBrushes.HighlightText, r);
                e.Handled = true;
            }
        }

        private DateTime sTime = DateTime.Now;
        private string strPerSelectGroupCaption;
        private string strNowSelDType;

        private string strPerSelectDName;

        /// <summary>
        /// 设备选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeList_device_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            TreeListNode node = e.Node;
            //if (node != null && node.Expanded)
            //    return;
            if (node != null && node.ParentNode != null)
            {
                //if (strNowDType == (string)node.ParentNode.GetValue(0))
                //    return;
                
                strNowDType = (string)node.ParentNode.GetValue(0);
                strNowDName = (string)node.GetValue(0);

                strNowSelDType = strNowDType;

                if (strNowDName == strPerSelectDName)
                    return;

                MySql.InsertUserScanT("设备浏览记录", strPerSelectGroupCaption +  GetChartName(), sTime, DateTime.Now, "实时值");

                strPerSelectGroupCaption = strNowSelDType;
                strPerSelectDName = strNowDName;
                //移除前面选择类型曲线
                foreach (string strMLine in paneDic.Keys)
                {
                    RemoveChartPanel(paneDic[strMLine]);
                }
                paneDic.Clear();
                mnl_list.Clear();
                kgl_list.Clear();

                listView_Pro.Items.Clear();
                int index = 0;
                int checkCount = 0;
                foreach (objMsgAttriStruct att in c_Device.DeviceMsgGather[strNowDType].ArrtiList)
                {
                    if (CheckAttiIsVisable(strNowDType, strNowDName, index) ||
                        ((CType == ChartType_Data.MONTH || CType == ChartType_Data.YEAR) && att.AnalogType != AnalogCodeType.None))
                    {
                        ListViewItem item = null;

                        if (att.AnalogType == AnalogCodeType.None)
                        {
                            if (CType == ChartType_Data.Real)
                            {
                                item = new ListViewItem();
                                item.SubItems.Add(att.AttriName);
                                item.Tag = "KGL";
                                listView_Pro.Items.Add(item);
                            }
                        }
                        else
                        {
                            item = new ListViewItem();
                            item.SubItems.Add(att.AttriName);
                            item.Tag = "MNL";
                            listView_Pro.Items.Add(item);
                        }
                     

                        if (checkCount < c_Device.DeviceMsgGather[strNowDType].ArrtiList.Count && checkCount < 2)
                        {
                            if (item != null && (string)item.Tag == "MNL")
                            {
                                item.Checked = true;
                                checkCount++;
                            }
                        }
                    }

                    index++;
                }
            }
            else if (node != null && !node.HasChildren)
            {
                //移除前面选择类型曲线
                foreach (string strMLine in paneDic.Keys)
                {
                    RemoveChartPanel(paneDic[strMLine]);
                }
                paneDic.Clear();
                mnl_list.Clear();
                kgl_list.Clear();

                listView_Pro.Items.Clear();
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            chartControl1.ShowPrintPreview();
        }

        private void chartControl1_CustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e)
        {

        }

        /// <summary>
        /// 年、月曲线时间选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MYTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            objDate data = new objDate();
            string strHead1 = string.Empty;
            string strHead2 = string.Empty;
            string strHead3 = string.Empty;
            List<SeriesPoint> points = new List<SeriesPoint>();
            DailyReportTable.objMonthCurve mCurve = new DailyReportTable.objMonthCurve();

            if (this.paneDic.Count > 0)
            {
                foreach (MultiChartPane pane in this.paneDic.Values)
                {
                    if (CType == ChartType_Data.MONTH)
                    { }
                    else if (CType == ChartType_Data.YEAR)
                    {
                        DateTime timeForX = new DateTime(DateTime.Parse((string)barEditItem_MYTime.SelectedItem).Year, 1, 1);

                        diagram.AxisX.DateTimeScaleOptions.AutoGrid = false;
                        diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
                        diagram.AxisX.DateTimeScaleOptions.GridSpacing = 30;
                        diagram.AxisX.WholeRange.Auto = false;
                        diagram.AxisX.WholeRange.SetMinMaxValues(timeForX, timeForX.AddMonths(12));

                        diagram.AxisX.VisualRange.Auto = false;
                        diagram.AxisX.VisualRange.SetMinMaxValues(timeForX, timeForX.AddMonths(12));
                        diagram.AxisX.Tickmarks.Visible = false;

                        DateTime dt_year = DateTime.Parse((string)barEditItem_MYTime.SelectedItem);
                        data.year = dt_year.Year;
                        data.month = (byte)dt_year.Month;
                        data.day = (byte)dt_year.Day;
                        drTable.ReadYearCurveValueList(data, strNowDType, strNowDName);
                        while (!drTable.Ready_YearCurveValueList)
                        {
                            if (drTable.MonthErrorFlag != 0)
                            {
                                return;
                            }

                            Thread.Sleep(20);
                        }

                        foreach (DailyReportTable.objMonthCurve dClass in drTable.YearCurveValueList[strNowDName])
                        {
                            if (dClass.AttriName == strNowAName)
                            {
                                mCurve = dClass;
                                break;
                            }
                        }

                        int seriesIndex = 0;
                        if (seriesIndex < pane.SeriesList.Count)
                        {
                            points = new List<SeriesPoint>();
                            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
                            {
                                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.MaxValue.Value);
                                points.Add(s);
                            }
                            pane.SeriesList[seriesIndex].Points.Clear();
                            AddPoints(pane.SeriesList[seriesIndex], points);
                            seriesIndex++;
                        }
                        if (seriesIndex < pane.SeriesList.Count)
                        {
                            points = new List<SeriesPoint>();
                            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
                            {
                                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.MinValue.Value);
                                points.Add(s);
                            }
                            pane.SeriesList[seriesIndex].Points.Clear();
                            AddPoints(pane.SeriesList[seriesIndex], points);
                            seriesIndex++;
                        }

                        if (seriesIndex < pane.SeriesList.Count)
                        {
                            points = new List<SeriesPoint>();
                            foreach (DailyReportTable.objMonthCurveValue tValue in mCurve.MonthCurveValueList)
                            {
                                SeriesPoint s = new SeriesPoint(new DateTime(tValue.year, tValue.month, tValue.day), tValue.Value.AvrValue.Value);
                                points.Add(s);
                            }
                            pane.SeriesList[seriesIndex].Points.Clear();
                            AddPoints(pane.SeriesList[seriesIndex], points);
                            seriesIndex++;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 自定义Y坐标显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Chart_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            //AxisBase axis = e.Item.Axis;
            //if (axis is AxisY || axis is AxisY3D || axis is RadarAxisY || axis is SecondaryAxisY)
            //{
            //    e.Item.Text = e.Item.Text + axis.Name;
            //}
        }
    }

    public class TreeListNodeObject
    {
        //public1005727395
    }

    public class ChartPro
    {
        public string strXLable = string.Empty;
        public string strYLable = string.Empty;
        public double alarmMax = 0;
        public double alarmMin = 0;
    }

    public class MNLValue
    {
        public DateTime x;
        public double y;
    }

    public class MultiChartPane
    {
        public XYDiagramPane pane;
        public Series series;
        public List<Series> SeriesList;
        public SecondaryAxisX myAxisX;
        public SecondaryAxisY myAxisY;
        public string strPaneName;
        public bool isKGL;

        /// <summary>
        /// 释放所有资源
        /// </summary>
        public void DisposeAll()
        {
            pane.Dispose();
            SeriesList.ForEach(s => s.Dispose());

            if (series != null)
                series.Dispose();
            if (myAxisX != null)
                myAxisX.Dispose();
            if (myAxisY != null)
                myAxisY.Dispose();
        }
    }

    /// <summary>
    /// 年月日曲线类别
    /// </summary>
    public enum ChartType_Data
    {
        DAY,
        MONTH,
        YEAR,
        Real
    }
}
